package com.shahe.GirlsDay.MyView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.widget.TextView;
import com.shahe.GirlsDay.R;
import com.shahe.GirlsDay.util.StaticClass;

import java.util.Calendar;
import java.util.Date;

public class BBTChartView extends View {

	private Paint paint;
	// private String date;
	// private Calendar calendar;
	private String temperature[] = new String[5];
	private TextView textView;
    String tempNum[][] = new String [5][2];
    int yValue[] = new int[5];
    int xValue[] = {200,300,400,500,600};

	public BBTChartView(Context context) {
		super(context);
		paint = new Paint();
		paint.setAntiAlias(true);
        getHLTemp();
	}

	public void onDraw(Canvas canvas) {
		// canvas.drawColor(Color.WHITE);
		drawAxis(canvas);
		drawChart(canvas);
	}

	public void drawAxis(Canvas canvas) {
		paint.setColor(getResources().getColor(R.color.chartLine));
		paint.setStrokeWidth(8);
		canvas.drawLine(100, 600, 680, 600, paint);// 横线
		canvas.drawLine(100, 80, 100, 600, paint);// 竖线

		paint.setColor(getResources().getColor(R.color.chartDottedLine));
		paint.setStrokeWidth(4);
		canvas.drawLine(100, 500, 680, 500, paint);// 横虚线
		canvas.drawLine(100, 400, 680, 400, paint);// 横虚线
		canvas.drawLine(100, 300, 680, 300, paint);// 横虚线
		canvas.drawLine(100, 200, 680, 200, paint);// 横虚线
		canvas.drawLine(100, 100, 680, 100, paint);// 横虚线

		canvas.drawLine(200, 80, 200, 600, paint);// 竖虚线
		canvas.drawLine(300, 80, 300, 600, paint);// 竖虚线
		canvas.drawLine(400, 80, 400, 600, paint);// 竖虚线
		canvas.drawLine(500, 80, 500, 600, paint);
		canvas.drawLine(600, 80, 600, 600, paint);// 竖虚线

		int x = 190;
		int y = 610;

		paint.setTextSize(30);
		paint.setColor(Color.BLACK);
		for (int i = 0; i < 5; i++) {
			// 横线
			canvas.drawText(StaticClass.temperatureDate[i], x, 640, paint);
			x += 100;
		}
		for (int i = 0; i < 6; i++) {
			// 竖线
			canvas.drawText(1.0 * (i + 35) + "", 30, y, paint);
			y -= 100;
		}
		canvas.drawText("°C", 60, 60, paint);
		canvas.drawText("Date", 660, 640, paint);

	}

	// 画曲线
	void drawChart(Canvas canvas) {
		paint.setColor(getResources().getColor(R.color.title_color));
		paint.setStrokeWidth(10);

        for (int i = 0; i < temperature.length; i++){
            tempNum[i] = temperature[i].split(".");
            yValue[i] = (41 - Integer.valueOf(tempNum[i][0]))*100 + Integer.valueOf(tempNum[i][1])*100;

            canvas.drawLine(yValue[i], xValue[i], yValue[i+1], xValue[i+1], paint);
        }

		//canvas.drawLine(100, 600, 200, 500, paint);// 横+50,竖-50；
		//canvas.drawLine(200, 500, 300, 400, paint);
		//canvas.drawLine(300, 400, 400, 500, paint);
		//canvas.drawLine(400, 500, 500, 400, paint);
	}

	public void getHLTemp() {
		float max = Float.parseFloat(temperature[0]);
		float min = Float.parseFloat(temperature[0]);
        setTemperatures();

		for (int i = 0; i < 5; i++) {

			if (max < Float.parseFloat(temperature[i])) {
				max = Float.parseFloat(temperature[i]);
			}
		}
		textView = (TextView) findViewById(R.id.tv_chart_l1);
		textView.setText(max + "");

		for (int i = 0; i < 5; i++) {

			if (min > Float.parseFloat(temperature[i])) {
				min = Float.parseFloat(temperature[i]);
			}
		}
		textView = (TextView) findViewById(R.id.tv_chart_r1);
		textView.setText(min + "");
	}

	public void setTemperatures() {
		if (StaticClass.dayList.size() >= 5) {
			for (int i = StaticClass.dayList.size() - 5; i < StaticClass.dayList
					.size(); i++) {
				int j = 0;
				int z = 0;
				StaticClass.temperatureDate[j] = StaticClass.DayInfoList.get(i)
						+ "";
				temperature[z] = StaticClass.DayInfoList.get(i)
						.getTemperature();
				j++;
				z++;

			}
		} else {
			for (int i = 0; i < StaticClass.dayList.size(); i++) {
				int j = 0;
				int z = 0;
				StaticClass.temperatureDate[j] = StaticClass.dayList.get(i)
						+ "";
				temperature[z] = StaticClass.DayInfoList.get(i)
						.getTemperature();
				j++;
				z++;
			}
		}
	}

}
